package com.example.blog.utils;

import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {
    private static final String jwtToken = "huamiaoyimaruidao!@$$";

    public static String createToken(Long id){
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",id);
        return   Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,jwtToken) //密钥为jwtToken
                .setClaims(claims)
                .setIssuedAt(new Date()) //设置签发时间
                .setExpiration(new Date(System.currentTimeMillis() + 3*60*60*60*1000))//三个小时的有效时间
                .compact();
    }

    public static Map<String,Object> checkToken(String token){
        try{
            return (Map<String, Object>) Jwts.parser().setSigningKey(jwtToken).parse(token).getBody();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
